home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BasicScrollPaneUI.java < prev    next >
Text File  |  1998-06-30  |  14KB  |  463 lines

  1. /*
  2.  * @(#)BasicScrollPaneUI.java    1.35 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.basic;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.event.*;
  25. import com.sun.java.swing.border.*;
  26. import com.sun.java.swing.plaf.*;
  27.  
  28. import java.awt.Component;
  29. import java.awt.Container;
  30. import java.awt.LayoutManager;
  31. import java.awt.Rectangle;
  32. import java.awt.Dimension;
  33. import java.awt.Point;
  34. import java.awt.Insets;
  35. import java.awt.Graphics;
  36. import java.io.Serializable;
  37.  
  38.  
  39. /**
  40.  * A Windows L&F implementation of ScrollPaneUI.
  41.  * <p>
  42.  * Warning: serialized objects of this class will not be compatible with
  43.  * future swing releases.  The current serialization support is appropriate 
  44.  * for short term storage or RMI between Swing1.0 applications.  It will
  45.  * not be possible to load serialized Swing1.0 objects with future releases
  46.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  47.  * baseline for the serialized form of Swing objects.
  48.  *
  49.  * @version 1.35 02/02/98
  50.  * @author Hans Muller
  51.  */
  52. public class BasicScrollPaneUI
  53.     extends ScrollPaneUI implements ScrollPaneConstants, Serializable
  54. {
  55.     protected JScrollPane scrollpane;
  56.     protected ChangeListener vsbListener = createVSBListener();
  57.     protected ChangeListener hsbListener = createHSBListener();
  58.     protected ChangeListener viewportListener = createViewportListener();
  59.  
  60.  
  61.     public void paint(Graphics g, JComponent c) {
  62.     Border vpBorder = scrollpane.getViewportBorder();
  63.     if (vpBorder != null) {
  64.         Rectangle r = getSPLayout().getViewportBorderBounds(scrollpane);
  65.         vpBorder.paintBorder(scrollpane, g, r.x, r.y, r.width, r.height);
  66.     }
  67.  
  68.     }
  69.  
  70.  
  71.     public Dimension getPreferredSize(JComponent c) {
  72.     return getSPLayout().preferredLayoutSize(c);
  73.     }
  74.  
  75.     public Dimension getMinimumSize(JComponent c) {
  76.     return getPreferredSize(c);
  77.     }
  78.  
  79.     public Dimension getMaximumSize(JComponent c) {
  80.     return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
  81.     }
  82.  
  83.  
  84.     public void installUI(JComponent x)
  85.     {
  86.     scrollpane = (JScrollPane)x;
  87.     scrollpane.setLayout(createLayoutManager());
  88.  
  89.     JViewport viewport = getViewport();
  90.     JScrollBar vsb = createVerticalScrollBar();
  91.     JScrollBar hsb = createHorizontalScrollBar();
  92.  
  93.     if (viewport != null) {
  94.         viewport.addChangeListener(viewportListener);
  95.     }
  96.  
  97.     vsb.getModel().addChangeListener(vsbListener);
  98.     hsb.getModel().addChangeListener(hsbListener);
  99.  
  100.     scrollpane.add(vsb, VERTICAL_SCROLLBAR);
  101.     scrollpane.add(hsb, HORIZONTAL_SCROLLBAR);
  102.  
  103.     LookAndFeel.installBorder(scrollpane, "ScrollPane.border");
  104.     LookAndFeel.installColorsAndFont( x, "ScrollPane.background", "ScrollPane.foreground", "ScrollPane.font");
  105.         Border vpBorder = scrollpane.getViewportBorder();
  106.         if ((vpBorder == null) ||( vpBorder instanceof UIResource)) {
  107.         vpBorder = UIManager.getBorder("ScrollPane.viewportBorder");
  108.         scrollpane.setViewportBorder(vpBorder);
  109.         }
  110.  
  111.     }
  112.  
  113.  
  114.     public void uninstallUI(JComponent x)
  115.     {
  116.     JViewport viewport = getViewport();
  117.     JScrollBar vsb = getVerticalScrollBar();
  118.     JScrollBar hsb = getHorizontalScrollBar();
  119.  
  120.     scrollpane.removeAll();
  121.     scrollpane.setLayout(null);
  122.  
  123.     if (viewport != null) {
  124.         viewport.removeChangeListener(viewportListener);
  125.     }
  126.  
  127.     if (vsb != null) {
  128.         vsb.getModel().removeChangeListener(vsbListener);
  129.     }
  130.  
  131.     if (hsb != null) {
  132.         hsb.getModel().removeChangeListener(hsbListener);
  133.     }
  134.  
  135.     LookAndFeel.uninstallBorder(scrollpane);
  136.  
  137.         if (scrollpane.getViewportBorder() instanceof UIResource) {
  138.             scrollpane.setViewportBorder(null);
  139.         }
  140.  
  141.     scrollpane = null;
  142.     }
  143.  
  144.  
  145.     public static ComponentUI createUI(JComponent x) {
  146.     return new BasicScrollPaneUI();
  147.     }
  148.  
  149.  
  150.     protected ScrollPaneLayout getSPLayout() {
  151.     return (ScrollPaneLayout)(scrollpane.getLayout());
  152.     }
  153.  
  154.     public int getVerticalScrollBarPolicy() {
  155.     return getSPLayout().getVerticalScrollBarPolicy();
  156.     }
  157.  
  158.     public void setVerticalScrollBarPolicy(int x) {
  159.     getSPLayout().setVerticalScrollBarPolicy(x);
  160.     }
  161.  
  162.     public int getHorizontalScrollBarPolicy() {
  163.     return getSPLayout().getHorizontalScrollBarPolicy();
  164.     }
  165.  
  166.     public void setHorizontalScrollBarPolicy(int x) {
  167.     getSPLayout().setHorizontalScrollBarPolicy(x);
  168.     }
  169.  
  170.     public JScrollBar getHorizontalScrollBar() {
  171.     return getSPLayout().getHorizontalScrollBar();
  172.     }
  173.  
  174.     public JScrollBar getVerticalScrollBar() {
  175.     return getSPLayout().getVerticalScrollBar();
  176.     }
  177.  
  178.     /**
  179.      * Remove the old viewport (if there is one), force the
  180.      * viewPosition of the new viewport to be in the +x,+y quadrant,
  181.      * sync up the row and column headers (if there are any) with the
  182.      * new viewport, and finally sync the scrollbars and
  183.      * headers with the new viewport.
  184.      */
  185.  
  186.     public void setViewport(JViewport newViewport) {
  187.     JViewport oldViewport = getViewport();
  188.  
  189.     if (newViewport == oldViewport) {
  190.         return;
  191.     }
  192.  
  193.     if (oldViewport != null) {
  194.         oldViewport.removeChangeListener(viewportListener);
  195.         scrollpane.remove(oldViewport);
  196.     }
  197.  
  198.     if (newViewport != null) {
  199.         Point p = newViewport.getViewPosition();
  200.         p.x = Math.max(p.x, 0);
  201.         p.y = Math.max(p.y, 0);
  202.         newViewport.setViewPosition(p);
  203.         scrollpane.add(newViewport, VIEWPORT);
  204.         syncScrollPaneWithViewport();
  205.         newViewport.addChangeListener(viewportListener);
  206.     }
  207.     }
  208.  
  209.     public JViewport getViewport() {
  210.     return getSPLayout().getViewport();
  211.     }
  212.  
  213.  
  214.     /**
  215.      * If an old rowHeader exists, remove it.  If the new rowHeader
  216.      * isn't null, sync the y coordinate of the its viewPosition with
  217.      * the viewport (if there is one) and then add it to the ScrollPane.
  218.      */
  219.  
  220.     public void setRowHeader(JViewport newRowHead) {
  221.     JViewport oldRowHead = getRowHeader();
  222.  
  223.     if (oldRowHead == newRowHead) {
  224.         return;
  225.     }
  226.  
  227.     if (oldRowHead != null) {
  228.         scrollpane.remove(oldRowHead);
  229.     }
  230.  
  231.     if (newRowHead != null) {
  232.         JViewport viewport = getViewport();
  233.         Point p = newRowHead.getViewPosition();
  234.         p.y = (viewport != null) ? viewport.getViewPosition().y : 0;
  235.         newRowHead.setViewPosition(p);
  236.         scrollpane.add(newRowHead, ROW_HEADER);
  237.     }
  238.     }
  239.  
  240.     public JViewport getRowHeader() {
  241.     return getSPLayout().getRowHeader();
  242.     }
  243.  
  244.  
  245.     /**
  246.      * If an old columnHeader exists, remove it.  If the new columnHeader
  247.      * isn't null, sync the x coordinate of the its viewPosition with
  248.      * the viewport (if there is one) and then add it to the ScrollPane.
  249.      */
  250.  
  251.     public void setColumnHeader(JViewport newColHead) {
  252.     JViewport oldColHead = getColumnHeader();
  253.  
  254.     if (oldColHead == newColHead) {
  255.         return;
  256.     }
  257.  
  258.     if (oldColHead != null) {
  259.         scrollpane.remove(oldColHead);
  260.     }
  261.  
  262.     if (newColHead != null) {
  263.         JViewport viewport = getViewport();
  264.         Point p = newColHead.getViewPosition();
  265.         p.x = (viewport != null) ? viewport.getViewPosition().x : 0;
  266.         newColHead.setViewPosition(p);
  267.         scrollpane.add(newColHead, COLUMN_HEADER);
  268.     }
  269.     }
  270.  
  271.     public JViewport getColumnHeader() {
  272.     return getSPLayout().getColumnHeader();
  273.     }
  274.  
  275.  
  276.  
  277.     public Component getCorner(String key) {
  278.     return getSPLayout().getCorner(key);
  279.     }
  280.  
  281.     public void setCorner(String key, Component x) {
  282.     scrollpane.add(x, key);
  283.     }
  284.  
  285.  
  286.     protected void syncScrollPaneWithViewport()
  287.     {
  288.     JViewport viewport = getViewport();
  289.     JScrollBar vsb = getVerticalScrollBar();
  290.     JScrollBar hsb = getHorizontalScrollBar();
  291.     JViewport rowHead = getRowHeader();
  292.     JViewport colHead = getColumnHeader();
  293.  
  294.     if (viewport != null) {
  295.         Dimension extentSize = viewport.getExtentSize();
  296.         Dimension viewSize = viewport.getViewSize();
  297.         Point viewPosition = viewport.getViewPosition();
  298.  
  299.         if (vsb != null) {
  300.         int extent = extentSize.height;
  301.         int max = viewSize.height;
  302.         int value = Math.max(0, Math.min(viewPosition.y, max - extent));
  303.         vsb.setValues(value, extent, 0, max);
  304.         }
  305.  
  306.         if (hsb != null) {
  307.         int extent = extentSize.width;
  308.         int max = viewSize.width;
  309.         int value = Math.max(0, Math.min(viewPosition.x, max - extent));
  310.         hsb.setValues(value, extent, 0, max);
  311.         }
  312.  
  313.         if (rowHead != null) {
  314.         Point p = rowHead.getViewPosition();
  315.         p.y = viewport.getViewPosition().y;
  316.         rowHead.setViewPosition(p);
  317.         }
  318.  
  319.         if (colHead != null) {
  320.         Point p = colHead.getViewPosition();
  321.         p.x = viewport.getViewPosition().x;
  322.         colHead.setViewPosition(p);
  323.         }
  324.     }
  325.     }
  326.  
  327.  
  328.     /**
  329.      * Listener for viewport events.
  330.      * <p>
  331.      * Warning: serialized objects of this class will not be compatible with
  332.      * future swing releases.  The current serialization support is appropriate
  333.      * for short term storage or RMI between Swing1.0 applications.  It will
  334.      * not be possible to load serialized Swing1.0 objects with future releases
  335.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  336.      * baseline for the serialized form of Swing objects.
  337.      */
  338.     protected class ViewportListener implements ChangeListener, Serializable
  339.     {
  340.     public void stateChanged(ChangeEvent e) {
  341.         syncScrollPaneWithViewport();
  342.     }
  343.     }
  344.  
  345.     protected ChangeListener createViewportListener() {
  346.     return new ViewportListener();
  347.     }
  348.  
  349.  
  350.     /**
  351.      * Horizontal scrollbar listener.
  352.      * <p>
  353.      * Warning: serialized objects of this class will not be compatible with
  354.      * future swing releases.  The current serialization support is appropriate
  355.      * for short term storage or RMI between Swing1.0 applications.  It will
  356.      * not be possible to load serialized Swing1.0 objects with future releases
  357.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  358.      * baseline for the serialized form of Swing objects.
  359.      */
  360.     protected class HSBListener implements ChangeListener, Serializable
  361.     {
  362.     public void stateChanged(ChangeEvent e)
  363.     {
  364.         JViewport viewport = getViewport();
  365.         if (viewport != null) {
  366.         BoundedRangeModel model = (BoundedRangeModel)(e.getSource());
  367.         Point p = viewport.getViewPosition();
  368.         p.x = model.getValue();
  369.         viewport.setViewPosition(p);
  370.         }
  371.     }
  372.     }
  373.  
  374.     protected ChangeListener createHSBListener() {
  375.     return new HSBListener();
  376.     }
  377.  
  378.  
  379.     /**
  380.      * Vertical scrollbar listener.
  381.      * <p>
  382.      * Warning: serialized objects of this class will not be compatible with
  383.      * future swing releases.  The current serialization support is appropriate
  384.      * for short term storage or RMI between Swing1.0 applications.  It will
  385.      * not be possible to load serialized Swing1.0 objects with future releases
  386.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  387.      * baseline for the serialized form of Swing objects.
  388.      */
  389.     protected class VSBListener implements ChangeListener, Serializable
  390.     {
  391.     public void stateChanged(ChangeEvent e)
  392.     {
  393.         JViewport viewport = getViewport();
  394.         if (viewport != null) {
  395.         BoundedRangeModel model = (BoundedRangeModel)(e.getSource());
  396.         Point p = viewport.getViewPosition();
  397.         p.y = model.getValue();
  398.         viewport.setViewPosition(p);
  399.         }
  400.     }
  401.     }
  402.  
  403.     protected ChangeListener createVSBListener() {
  404.     return new VSBListener();
  405.     }
  406.  
  407.  
  408.     /**
  409.      * Standard layout manager for a scroll pane.
  410.      * <p>
  411.      * Warning: serialized objects of this class will not be compatible with
  412.      * future swing releases.  The current serialization support is appropriate
  413.      * for short term storage or RMI between Swing1.0 applications.  It will
  414.      * not be possible to load serialized Swing1.0 objects with future releases
  415.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  416.      * baseline for the serialized form of Swing objects.
  417.      */
  418.     protected class BasicScrollPaneLayout extends ScrollPaneLayout implements Serializable
  419.     {
  420.     public void layoutContainer(Container parent) {
  421.         super.layoutContainer(parent);
  422.         syncScrollPaneWithViewport();
  423.     }
  424.     }
  425.  
  426.     /**
  427.      * @return new BasicScrollPaneLayout()
  428.      */
  429.     protected LayoutManager createLayoutManager() {
  430.     return new BasicScrollPaneLayout();
  431.     }
  432.  
  433.  
  434.     /**
  435.      * This method just delegates to the JScrollPane
  436.      * <code>createVerticalScrollbar()</code> method.  Subclasses
  437.      * may override this method to wrap the scrollbar with a
  438.      * special proxy or reconfigure the scrollbar, e.g.
  439.      * by setting its border.
  440.      *
  441.      * @returns scrollpane.createVerticalScrollBar()
  442.      * @see JScrollPane#createVerticalScrollBar
  443.      */
  444.     protected JScrollBar createVerticalScrollBar() {
  445.     return scrollpane.createVerticalScrollBar();
  446.     }
  447.  
  448.     /**
  449.      * This method just delegates to the JScrollPane
  450.      * <code>createVerticalScrollbar()</code> method.  Subclasses
  451.      * may override this method to wrap the scrollbar with a
  452.      * special proxy or reconfigure the scrollbar, e.g. by
  453.      * setting its border.
  454.      *
  455.      * @returns scrollpane.createHorizontalScrollBar()
  456.      * @see JScrollPane#createHorizontalScrollBar
  457.      */
  458.     protected JScrollBar createHorizontalScrollBar() {
  459.     return scrollpane.createHorizontalScrollBar();
  460.     }
  461.  
  462. }
  463.